package cn.edu.cjw.APMFP.ui.panel;

import cn.edu.cjw.APMFP.Util.CacheUtil;
import cn.edu.cjw.APMFP.Util.StringUtil;
import cn.edu.cjw.APMFP.ui.CenterPanel;
import cn.edu.cjw.APMFP.ui.component.VFlowLayout;
import cn.edu.cjw.APMFP.user.Service.UserService;
import cn.edu.cjw.APMFP.user.Service.UserServiceImpl;
import cn.edu.cjw.APMFP.user.pojo.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @package cn.edu.cjw.APMFP.ui.panel
 * @className PersonPanel
 * @note 个人面板
 * @author wind
 * @date 2022/3/12 13:12
 */
public class PersonUpdatePanel extends JPanel {

    private CenterPanel mainPanel;

    private UserService service = new UserServiceImpl();

    public PersonUpdatePanel(CenterPanel mainPanel) {
        this.mainPanel = mainPanel;
        this.init();
    }

    public void init(){
        User user = CacheUtil.getUser(mainPanel);
        GridBagLayout gridBagLayout = new GridBagLayout();
        this.setLayout(gridBagLayout);

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.insets = new Insets(10, 10, 10, 10);
        constraints.ipady = 10;

        JLabel accountLabel = new JLabel("账号:");
        JTextField accountField = new JTextField(27);
        accountField.setEnabled(false);
        constraints.gridx = 0;
        constraints.gridy = 0;
        this.add(accountLabel, constraints);
        constraints.gridx = 1;
        this.add(accountField, constraints);

        JLabel pwdLabel = new JLabel("密码:");
        JPasswordField pwdField = new JPasswordField(27);
        constraints.gridx = 0;
        constraints.gridy = 1;
        this.add(pwdLabel, constraints);
        constraints.gridx = 1;
        this.add(pwdField, constraints);

        JLabel nameLabel = new JLabel("名称:");
        JTextField nameField = new JTextField(27);
        constraints.gridx = 0;
        constraints.gridy = 2;
        this.add(nameLabel, constraints);
        constraints.gridx = 1;
        this.add(nameField, constraints);

        JLabel genderLabel = new JLabel("性别:");
        JComboBox<String> genderField = new JComboBox<>();
        genderField.setPreferredSize(nameField.getPreferredSize());
        genderField.addItem("男");
        genderField.addItem("女");
        constraints.gridx = 0;
        constraints.gridy = 3;
        this.add(genderLabel, constraints);
        constraints.gridx = 1;
        this.add(genderField, constraints);

        JLabel ageLabel = new JLabel("年龄:");
        JTextField ageField = new JTextField(27);
        constraints.gridx = 0;
        constraints.gridy = 4;
        this.add(ageLabel, constraints);
        constraints.gridx = 1;
        this.add(ageField, constraints);

        JButton button = new JButton("保存");
        constraints.gridx = 0;
        constraints.gridy = 5;
        this.add(button, constraints);

        accountField.setText(user.getAccount());
        pwdField.setText(user.getPassWord());
        nameField.setText(user.getName());
        genderField.setSelectedItem(user.getGender());
        ageField.setText(user.getAge() + "");

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = nameField.getText().trim();
                if(!StringUtil.isNotBlank(name)){
                    JOptionPane.showMessageDialog(null, "请输入名称");
                    return;
                }
                String pwd = new String(pwdField.getPassword()).trim();
                if(!StringUtil.isNotBlank(pwd)){
                    JOptionPane.showMessageDialog(null, "请输入密码");
                    return;
                }
                Object sex = genderField.getSelectedItem();
                if(sex == null){
                    JOptionPane.showMessageDialog(null, "请选择性别");
                    return;
                }
                String age = ageField.getText().trim();
                if(!StringUtil.isInt(age)){
                    JOptionPane.showMessageDialog(null, "年龄不合法");
                    return;
                }
                try {
                    service.updateUser(user, pwd, name, user.getuRole(), sex.toString(), Integer.parseInt(age), user.isDeactivate());
                    user.setPassWord(pwd);
                    user.setName(name);
                    user.setGender(sex.toString());
                    user.setAge(Integer.parseInt(age));
                    CacheUtil.login(user);
                    JOptionPane.showMessageDialog(null, "保存成功");
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, "保存失败");
                }
            }
        });
    }
}
